package handlers

import (
	"github.com/gin-gonic/gin"
	"github.com/goccy/go-json"
	"order-man/cache"
	"order-man/models"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	carCode := c.PostForm("car_code")
	carName := c.PostForm("car_name")
	carPeople := c.PostForm("car_people")
	if carCode == "" || carName == "" || carPeople == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Car{
		CarCode:   carCode,
		CarName:   carName,
		CarPeople: carPeople,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderAdd(c *gin.Context) {
	orderCode := c.PostForm("order_code")
	beginAddress := c.PostForm("begin_address")
	toAddress := c.PostForm("to_address")
	orderName := c.PostForm("order_name")
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	if orderCode == "" || beginAddress == "" || toAddress == "" || orderName == "" || cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Order{
		OrderCode:    orderCode,
		BeginAddress: beginAddress,
		ToAddress:    toAddress,
		OrderName:    orderName,
		Cid:          cid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var total int64
	models.DB.Model(models.Order{}).Count(&total)
	var list []models.OrderList
	redis_key := "car_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select("orders.id AS OrderID",
				"cars.car_code AS CarCode",
				"cars.car_name AS CarName",
				"cars.car_people AS CarPeople",
				"orders.order_code AS OrderCode",
				"orders.begin_address AS BeginAddress",
				"orders.to_address AS ToAddress",
				"orders.order_name AS OrderName").
			Joins("LEFT JOIN cars ON orders.cid = cars.id").
			Offset(offset).
			Limit(size).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	c.JSON(200, gin.H{
		"code":  500,
		"msg":   "列表查询成功",
		"data":  list,
		"total": total,
	})
	return

}
func OrderDetail(c *gin.Context) {
	id := c.Query("id")
	var order models.OrderList
	redis_key := "car_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select("orders.id AS OrderID",
				"cars.car_code AS CarCode",
				"cars.car_name AS CarName",
				"cars.car_people AS CarPeople",
				"orders.order_code AS OrderCode",
				"orders.begin_address AS BeginAddress",
				"orders.to_address AS ToAddress",
				"orders.order_name AS OrderName").
			Joins("LEFT JOIN cars ON orders.cid = cars.id").
			Where("orders.id=?", id).
			Find(&order)
		jsonData, _ := json.Marshal(order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &order)
	}
	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "详情查询成功",
		"data": order,
	})
	return

}
